package com.guanyou;

import com.guanyou.lock.IDLock;
import com.guanyou.lock.LockThread;
import com.guanyou.lock.LockThread2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import sun.misc.Signal;
import sun.misc.SignalHandler;

@EnableScheduling
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MainEntrance {

    private static final Logger logger = LogManager.getLogger(MainEntrance.class);

    public static void main(String[] args) throws InterruptedException {

        ConfigurableApplicationContext sc = SpringApplication.run(MainEntrance.class, args);

        IDLock lock = sc.getBean(IDLock.class);

        SignalHandler sh = new SignalHandler() {
            @Override
            public void handle(Signal signal) {

                System.out
                        .println(signal.getName() + ".." + signal.getNumber());

                lock.unlockAll();


            }
        };

        Signal.handle(new Signal(System.getProperties().getProperty("os.name")
                .toLowerCase().startsWith("win") ? "INT" : "TERM"), sh);

//        lock.threadId.get();

        LockThread lockThread = new LockThread(lock, 1);
        lockThread.start();

        Thread.sleep(5000L);

        LockThread2 lockThread2 = new LockThread2(lock, 2);
        lockThread2.start();


        Thread.sleep(1000 * 2L);
//        System.exit(0);

        lockThread.join();
        lockThread2.join();
    }

}